<?php
namespace app\controller;

use app\BaseController;
use app\model\Album;
use app\model\AlbumPhoto;
use app\model\User;
use app\OssClient;
use app\Request;
use think\facade\Db;

class AlbumController extends BaseController{

    /**
     * 查询相册
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findAlbum(Request $request){
        //获取用户id
        $uid = $request->user_id;
        //需要查找的相册信息
        $needFindAlbumInfo = ['id','album_name','album_quantity','album_hot','create_time','update_time'];
        //查找出相册信息
        $findAlbumInfo = Album::where('user_id','=',$uid)->field($needFindAlbumInfo)->select();
        //将相册信息循环，用相册id去查找相册里面的照片
        foreach ($findAlbumInfo as $k => $v){
            //利用相册id查找出里面的一张近期照片
            $v['photo'] = AlbumPhoto::where($v['id'],'=','album_id')->field(['photo','photo_url'])->order('upload_time desc')->limit(1)->select();
        }
        return $this->returnJson(self::SUCCESS,'成功',$findAlbumInfo);
    }

    /**
     * 查找照片
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function findPhoto(Request $request){
        //获取相册id
        $aid = $request->get('album_id');
        $ossClient = new OssClient();
        $ossClient->upload();
        //查找出当前相册的所有照片，循环，并将用户名也给查找出来
        $findPhotoInfo = AlbumPhoto::where('album_id','=',$aid)->field(['user_id','photo','photo_url','upload_time'])->order('upload_time desc')->select();
        foreach ($findPhotoInfo as $k => $v){
            $v['username'] = User::where('id','=',$v['user_id'])->value('name');
        }
        return $this->returnJson(self::SUCCESS,'成功',$findPhotoInfo);
    }

    /**
     * 上传照片(至相册
     * @param Request $request
     * @return mixed
     */
    public function addPhoto(Request $request){
        //获取当前用户id
        $uid = $request->user_id;
        //获取前端传来的值
        $getData = $request->param(['album_id','photo_name']);
        $img = $request->file("img");

        Db::startTrans();//开启事物
        try {
            $ossClient = new OssClient();
            $fileName = md5(time()).md5($uid).".png";
            $folder = 'album/'.$getData['album_id'];

            $path = env("askey.url").$folder. '/'.$fileName;
            $ossClient->upload($img->getPathname(),$fileName,$folder);

            AlbumPhoto::create([
                'album_id'      => $getData['album_id'],
                'user_id'       => $uid,
                'photo'         => $getData['photo_name'],
                'photo_url'     => $path,
                'upload_time'   => time()
            ]);

            return $this->returnJson(self::SUCCESS,'上传成功','');
            Db::commit();//提交事物
        }catch (\Exception $exception){
            Db::rollback();//回滚事物

            return $this->returnJson(self::SERVER_ERROR,'系统错误',$exception);

        }
    }
}